<div class="grid_12">
    <div class="box round first fullpage">
        <h2>Các danh mục con của "<?=$this->p_row->category_name?>"</h2>
        <div class="block" style="position: relative;">
            <!-- If( cho phep them danh mục con) -->
            <?php if($this->c_option['allow_add'] && $this->rule->allow_add){?>
            <div class="vbutton">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"article-category","action"=>"add-subcat"))?>" class="btn btn-black add" style="margin-bottom: 20px;">Thêm danh mục con</a>
            </div>
            <?php }?>
            <!-- End If -->
            <div class="message" style="display: none;">
                <h5>Warning!</h5>
                <p>
                    This is a warning message.
                </p>
            </div>
            <div class="dataTables_wrapper">
                <table class="data display datatable">
                    <thead>
                        <tr>
                            <th width="40" class="center">STT</th>
                            <th class="left">Tên danh mục con</th>
                            
                            <?php if($this->c_option['allow_update'] && $this->rule->allow_edit){?>                            
                            <th width="50" class="center">Sửa</th>
                            <?php }?>
                            
                            <?php if($this->c_option['allow_del'] && $this->rule->allow_del){?>
                            <th width="50" class="center">Xóa</th>
                            <?php }?>
                            
                            <th width="70" class="center">local</th>
                        </tr>                   
                    </thead>
                    
                    <tbody>
                    <?php $stt=1; foreach($this->c_rows as $row){ $parity = ($stt%2)?"odd":"even";?>
                        <tr class="gradeU <?=$parity?>">
                            <td><?=$stt;?><input type="hidden" value="<?=$row->idcategories?>" /></td>
                            <td class="zend_form">
                                <span class="cat-name"><?=$row->category_name?></span>
                                <span class="adm-name" style="display: none;"><?=$row->admin_name?></span>
                            </td>
                            
                            <?php if($this->c_option['allow_update'] && $this->rule->allow_edit){?>
                            <td>
                                <a href="#" class="action-edit">
                                    <img src="<?=$this->baseUrl("/templates/admin/img/icon-edit.png")?>" alt="edit" title="Sửa" />
                                </a>
                                <a href="<?=$this->url(array("module"=>"admin","controller"=>"article-category","action"=>"edit-subcat","id"=>$row->idcategories))?>" class="action-save" style="display: none; color: blue;">
                                    [Lưu]
                                </a>
                                <a href="#" class="action-cancel" style="display: none; color: red;">[Hủy]</a>
                            </td>
                            <?php }?>
                            
                            <?php if($this->c_option['allow_del'] && $this->rule->allow_del){?>
                            <td>
                                <a href="<?=$this->baseUrl("admin/article-category/del-subcat/idcat/$row->idcategories")?>" class="action-del">
                                    <img src="<?=$this->baseUrl("/templates/admin/img/icon-del.png")?>" alt="delete" title="Xóa" />
                                </a>
                            </td>
                            <?php }?>
                            <td>
                                <input class="change-local" type="text" value="<?=$row->category_local?>" size="2"/>
                            </td>
                        </tr>
                    <?php $stt++;}?>
                    </tbody>
                </table>
            </div>
                     
        </div>
    </div>
</div>
<div class="dialog-message" title="Thông báo" style="display: none;"></div>

<script>
$(function(){
    //delete item
    $(".action-del").click(function(){
        var link = $(this).attr("href");
        var _click = $(this);
        $(".dialog-message").html("<div class='message warning'><h5>Cảnh báo</h5><p>Bạn có chắc muốn thực hiện hành động này không?</p></div>")
                            .dialog({
            autoOpen: true,
            modal: true,
            buttons:{
                "Có": function(){
                    $.get(link,null,function(rdata){
                        if(rdata!=0){
                            var text = "Xóa danh mục thành công.";
                            switch(rdata){
                                case 1: text = "Tồn tại danh mục con, không thể xóa."; break;
                                case 2: text = "Tồn tại bài viết trong danh mục này, không thể xóa."; break;
                                case 3: text = "Lỗi database."
                            }
                            $(".message").addClass("warning").show();
                            $(".message h5").text("Cảnh báo!");
                            $(".message p").text(text);
                            window.location = "javascript:scroll(0,0)";
                            setInterval(function(){
                                $(".message").fadeOut(2000)
                            },3000);
                        }else{
                            _click.parent("td").parent("tr").remove();
                            window.location = window.location.href;
                        }
                    },"json");
                    $(this).dialog("close");
                },                
                "Không": function(){
                    $(this).dialog("close");
                }
            }
            
        })
        return false;
    });
    
    // change local
    $(".change-local").change(function(){
        var lc = $(this).val();
        var id = $(this).parent("td").parent("tr")
                        .find("td:first")
                        .children("input[type='hidden']").val();
        var link = "<?=$this->url(array('module'=>"admin",'controller'=>"article-category",'action'=>"list"),null, true)?>";
        $.post(link,{"act":"local","idcat":id,"local":lc},function(){},"json");
    })
    
    //edit row
    $(".action-edit").click(function(){
        $(this).next().next("a.action-cancel").show();
        $(this).hide().next("a.action-save").show();
        var cat_name = $(this).parent("td").prev("td").children("span.cat-name").text();
        var adm_name = $(this).parent("td").prev("td").children("span.adm-name").text();
        var html = "<label class='required'>Tên danh mục:</label> <input rel='"+cat_name+"' class='icat-name' size='100' type='text' value='"+cat_name+"' /><span class='error'></span> <br />";
            html+= "<label>Tên quản trị:</label> <input rel='"+adm_name+"' class='iadm-name' size='100' type='text' value='"+adm_name+"' />";
        $(this).parent("td").prev("td").html(html);
        return false;
    });
    //save row
    $(".action-save").click(function(){
        var o_cat_name = $(this).parent("td")
                                .prev("td")
                                .children("input.icat-name");
        var icat_name = o_cat_name.val();
        var iadm_name = $(this).parent("td")
                               .prev("td")
                               .children("input.iadm-name").val();
        iadm_name = (iadm_name=="")?icat_name:iadm_name;
        
        var link = $(this).attr("href");
        if(icat_name==""){
            o_cat_name.addClass("error");
            o_cat_name.next("span.error").text(" Tên danh mục không được để trống.");
            
        }else{
            $(".message h5").remove();
            $(".message p").text("Saving...");
            $(".message").addClass("loading")
                         .addClass("warning").show();
            $.get(link,{"cat_name":icat_name,"adm_name":iadm_name},function(rdata){
                window.location = window.location.href;
            },"json");
        }
        
        return false;
    });
    
    // cancel click
    $(".action-cancel").click(function(){
        var cat_name = $(this).parent("td").prev("td").children("input.icat-name").attr("rel");
        var adm_name = $(this).parent("td").prev("td").children("input.iadm-name").attr("rel");
        var html = "<span class='cat-name'>"+cat_name+"</span><span class='adm-name' style='display: none;'>"+adm_name+"</span>";
        $(this).parent("td").prev("td").html(html);
        $(this).hide();
        $(this).prev(".action-save").hide().prev(".action-edit").show();
        return false;
    });
})
</script>